package com.ys.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.common.result.Result;
import com.ys.modules.system.entity.Role;
import com.ys.modules.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("分页查询角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "返回角色分页列表", response = Role.class, responseContainer = "Page")
    })
    @GetMapping("/page")
    public Result<IPage<Role>> page(
        @ApiParam(value = "页码", required = true) @RequestParam int pageNum,
        @ApiParam(value = "每页数量", required = true) @RequestParam int pageSize) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        return Result.success(roleService.page(page));
    }

    @ApiOperation("角色详情")
    @ApiResponses({
        @ApiResponse(code = 200, message = "返回角色详情", response = Role.class)
    })
    @GetMapping("/{id}")
    public Result<Role> detail(@ApiParam(value = "角色ID", required = true) @PathVariable Long id) {
        return Result.success(roleService.getById(id));
    }

    @ApiOperation("新增角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "新增成功", response = Boolean.class)
    })
    @PostMapping
    public Result<Boolean> add(@ApiParam(value = "角色实体", required = true) @RequestBody Role role) {
        return Result.success(roleService.save(role));
    }

    @ApiOperation("修改角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "修改成功", response = Boolean.class)
    })
    @PutMapping
    public Result<Boolean> update(@ApiParam(value = "角色实体", required = true) @RequestBody Role role) {
        return Result.success(roleService.updateById(role));
    }

    @ApiOperation("删除角色")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功", response = Boolean.class)
    })
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@ApiParam(value = "角色ID", required = true) @PathVariable Long id) {
        return Result.success(roleService.removeById(id));
    }
} 